package com.hw.backapi.controller;

import com.hw.backapi.service.IAdminService;
import com.hw.common.annotation.MyLogAdmin;
import com.hw.common.contants.UrlContants;
import com.hw.model.common.BaseResult;
import com.hw.model.dto.PageInfoDTO;
import com.hw.model.dto.user.LoginDTO;
import com.hw.model.dto.user.UserSaveDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 后台用户控制器
 */
@RestController
public class UserAdminController {

    @Autowired
    IAdminService adminServiceImpl;

    /**
     * 登录
     * @param loginDTO
     * @throws Exception
     */
    @PostMapping(value = UrlContants.LOGIN)
    @MyLogAdmin(label = "登录")
    public BaseResult login(
            @RequestBody LoginDTO loginDTO
    ) throws Exception {
        return BaseResult.ok(adminServiceImpl.login(loginDTO));
    }

    /**
     * 退出登录
     */
    @GetMapping(UrlContants.LOGOUT)
    @MyLogAdmin(label = "退出登录")
    public BaseResult logout(){
        adminServiceImpl.logout();
        return BaseResult.ok();
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping(UrlContants.GET_USER_INFO)
    @MyLogAdmin(label = "获取用户信息")
    public BaseResult getUserInfo(){
        return BaseResult.ok(adminServiceImpl.getUserInfo());
    }

    /**
     * 更新用户信息
     * @param userSaveDTO
     * @return
     */
    @PostMapping(UrlContants.UPDATE_USER_INFO)
    @MyLogAdmin(label = "更新用户信息")
    public BaseResult updateUserInfo(
            @RequestBody UserSaveDTO userSaveDTO
            ){
        adminServiceImpl.updateUserInfo(userSaveDTO);
        return BaseResult.ok();
    }

    /**
     * 增加用户
     * @param userSaveDTO
     * @return
     */
    @PostMapping(UrlContants.ADD_USER_INFO)
    @MyLogAdmin(label = "增加用户")
    public BaseResult addUserInfo(
            @RequestBody UserSaveDTO userSaveDTO
    ){
        adminServiceImpl.addUserInfo(userSaveDTO);
        return BaseResult.ok();
    }

    /**
     * 更新角色
     * @param userSaveDTO
     * @return
     */
    @PostMapping(UrlContants.UPDATE_ROLE)
    @MyLogAdmin(label = "更新角色")
    public BaseResult updateRole(
            @RequestBody UserSaveDTO userSaveDTO
    ){
        adminServiceImpl.updateRole(userSaveDTO);
        return BaseResult.ok();
    }

    /**
     * 用户列表
     * @return
     */
    @PostMapping(UrlContants.GET_USER_PAGE_LIST)
    public BaseResult getUserPageList(
            @RequestBody PageInfoDTO pageInfoDTO
            ){
        return BaseResult.ok(adminServiceImpl.getUserPageList(pageInfoDTO));
    }

    /**
     * 删除用户
     */
    @GetMapping(UrlContants.DEL_USER)
    @MyLogAdmin(label = "删除用户")
    public BaseResult delUser(
            @RequestParam("id") Long id
    ){
        adminServiceImpl.delUserInfo(id);
        return BaseResult.ok("删除成功");
    }

}
